﻿using Mimas.Core.Current;
using Mimas.Framework.Shared.Data.DataTransferObjects;
using Mimas.Framework.Shared.Data.EnumObjects;
using System;
using System.Linq;

namespace Mimas.Framework.Server.DomainObjects.Mapping
{
    public static class PersonMapping
    {
        public static Person Map(PersonDTO data)
        {
            Person newItem = new Person();

            newItem.BirthDate = data.BirthDate;
            newItem.CountryId = data.CountryId;
            newItem.Id = data.Id;
            newItem.IdentityNo = data.IdentityNo;
            newItem.Name = data.Name;
            newItem.NativeLanguageId = data.NativeLanguage.HasValue ? (int?)data.NativeLanguage : (int?)null;
            newItem.Surname = data.Surname;
            newItem.UserId = data.UserId;

            if (data.Id == 0)
            {
                newItem.CreatedBy = UserInfo.CurrentUser.UserId;
                newItem.CreatedOn = DateTime.Now;
            }
            else
            {
                newItem.ModifiedBy = UserInfo.CurrentUser.UserId;
                newItem.ModifiedOn = DateTime.Now;
            }

            return newItem;
        }

        public static PersonDTO Map(Person data)
        {
            PersonDTO newItem = new PersonDTO();

            newItem.BirthDate = data.BirthDate;
            newItem.CountryId = data.CountryId;
            newItem.Id = data.Id;
            newItem.IdentityNo = data.IdentityNo;
            newItem.Name = data.Name;
            newItem.NativeLanguage = (ELanguage?)data.NativeLanguageId;
            newItem.Surname = data.Surname;
            newItem.UserId = data.UserId;

            return newItem;
        }


        public static Person[] Map(PersonDTO[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }

        public static PersonDTO[] Map(Person[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }
    }
}
